package Frame.shopSet;

import FCRendering.FCRMaster;
import FCRendering.sw.FCRValues;
import Frame.entity.UnifyShopSayUIEntity;
import VWorldGameStudio.*;
import lombok.Setter;
import Function.FCItemForTankHelper;
import Function.FCMutilPlayCache;

import java.util.Vector;

import static FCRendering.FCRMaster.*;
import static FCRendering.FCRMaster.player1;

/**
 * 维修服务界面和功能
 */
public class FunctionRepairShop extends AShopFuntion {
    //修理报价,要是只是损的报价一半,

    private Vector<FixTank> needRepairNames = new Vector<>();

    private int showFristIndex = 0;
    @Setter
    private int endI = 0;

    private int needAllMoney = 0;
    private boolean isNpcDad = false;

    public FunctionRepairShop(){
        buildShopWindow();
    }

    public void buildShopWindow() {
        if ("LaDuo-LaoBa".equals(fcv.getSayNpcName())) {
            isNpcDad = true;
        }
        FCTank tank = tankLib.getTankOfID(player1.getPlayertank1(), player1.getPlayertankHostId());
        if (tank != null) {
            checkAndAdd(tank);
        }

        //这里帮了别人的坦克修理完了,先限制
        for (FCRPlayerLink link : FCMutilPlayCache.getTeamMembers()) {
            if(!link.curPlayerHostUID.equals(gameSave.HOST_UID)) {
                continue;
            }
            FCPlayerInfo p = FCMutilPlayCache.findPlayerByName(link.curPlayerHostUID, link.curPlayerName);
            if (p != null) {
                tank = tankLib.getTankOfID(p.getPlayertank1(), p.getPlayertankHostId());
                if (tank != null) {
                    checkAndAdd(tank);
                }
            }
        }
        //还有牵引的
        for (FCPropulsion fc : fcv.getPropulsions()) {
            if(fc.getPropulsionHostId().equals(gameSave.HOST_UID) && fc.getPropulsionType().equals("tank")){
                checkAndAdd((FCTank) fc);
            }
        }

        setEndI(Math.min(needRepairNames.size(), showFristIndex + 6));
        resetUI();
    }

    private void resetUI() {
        FCRValues.resetUnifySayUIEntity();
        FCRValues.leftWindowUI[0].leftText = "全部修理";
        FCRValues.leftWindowUI[0].rightText = needAllMoney+"G";
        int m = 1;
        for (int i = showFristIndex; i < endI; i++) {
            FixTank fixTank = needRepairNames.get(i);
            FCRValues.leftWindowUI[m].leftText = fixTank.text;
            FCRValues.leftWindowUI[m].rightText = "单修"+fixTank.needFixMoney+"G";
            m++;
        }
        if(isNpcDad){
            FCRValues.rightWindowUI[0].leftText = "赶紧修理完就滚吧，去做你的大侠梦！";
        }else {
            FCRValues.rightWindowUI[0].leftText = "车就是男人的面子，修好啥都好！";
        }

        FCRValues.rightWindowUI[4].rightText = "上一页";
        FCRValues.rightWindowUI[5].rightText = "下一页";
        FCRValues.rightWindowUI[6].rightText = "不修理了";
    }

    private void checkAndAdd(FCTank tank) {
        checkAndAdd2(tank.getGunID(), "主炮", tank);
        checkAndAdd2(tank.getGun2ID(), "副炮", tank);
        checkAndAdd2(tank.getSEID(), "SE", tank);
        checkAndAdd2(tank.getChassisID(), "底盘", tank);
        checkAndAdd2(tank.getCdeviceID(), "C装置", tank);
        checkAndAdd2(tank.getRadarID(), "雷达", tank);
        checkAndAdd2(tank.getEngineID(), "引擎", tank);
    }

    private void checkAndAdd2(String itemN, String tag, FCTank tank) {
        FCItem item = FCRMaster.itemLib.getItemOfID(itemN);
        if (item != null) {
            int needMoney = item.getRepairPrice();
            if(item.getDefenseAdd() > 0) {//表示这个东西甲
                int allDefense = item.getDefenseAdd()+item.getDefenseUpgradeAdd();
                if (item.getCurrentDefense() <= 0) {
                    needAllMoney += (isNpcDad ? 0 : needMoney);
                    needRepairNames.add(new FixTank(itemN, tank, isNpcDad ? 0 : needMoney, langLib.getNameOfId(tank.getName()) + "-" + tag + "-坏"));
                } else if (item.getCurrentDefense() <= allDefense / 3) {
                    needAllMoney += (isNpcDad ? 0 : needMoney / 2);
                    needRepairNames.add(new FixTank(itemN, tank, isNpcDad ? 0 : needMoney / 2, langLib.getNameOfId(tank.getName()) + "-" + tag + "-大损"));
                } else if (item.getCurrentDefense() <= allDefense / 2) {
                    needAllMoney += (isNpcDad ? 0 : needMoney / 3);
                    needRepairNames.add(new FixTank(itemN, tank, isNpcDad ? 0 : needMoney / 3, langLib.getNameOfId(tank.getName()) + "-" + tag + "-小损"));
                }
            }
        }
    }



    @Override
    public void clickLeftWindow(int i, UnifyShopSayUIEntity entity) {

        if(i == 0){
            //全部修理
            if(player1.getMoney() >= needAllMoney){
                if(needRepairNames.size() == 0){
                    FCRValues.rightWindowUI[1].leftText = "你的战车都毛问题，不需要修理";
                }else {
                    for (FixTank f : needRepairNames) {
                        fixTankDeal(f);
                    }
                    needRepairNames.clear();
                    setEndI(Math.min(needRepairNames.size(), showFristIndex + 6));
                    resetUI();
                    FCRValues.rightWindowUI[1].leftText = "全部修理完成";
                }
            }else {
                FCRValues.rightWindowUI[1].leftText = "你的金币好像不太够喔！";
            }
        }else {
            //单独修理
            if(entity.leftText.length() > 1) {
                FixTank fixTank = needRepairNames.get(showFristIndex + i - 1);
                fixTankDeal(fixTank);
                needRepairNames.remove(fixTank);
                setEndI(Math.min(needRepairNames.size(), showFristIndex + 6));
                resetUI();
                FCRValues.rightWindowUI[1].leftText = "修理完成，请继续";
            }
        }
    }

    private void fixTankDeal(FixTank fixTank){
        if(player1.getMoney() >= fixTank.needFixMoney){
            FCItem item = itemLib.getItemOfID(fixTank.itemName);
            if(item != null){
                FCItemForTankHelper.fixItem(item);
                player1.setMoney(player1.getMoney() - fixTank.needFixMoney);
                fixTank.tank.reInitShuxing();
            }
        }else {
            FCRValues.rightWindowUI[1].leftText = "你的金币好像不太够喔！";
        }
    }

    @Override
    public void clickRightWindow(int i, UnifyShopSayUIEntity entity) {
        setEndI(Math.min(needRepairNames.size(), showFristIndex + 6));//保留这句代码
        switch (i) {
            case 4:
                if (showFristIndex >= 6) {
                    showFristIndex -= 6;
                    setEndI(Math.min(needRepairNames.size(), showFristIndex + 6));
                    resetUI();
                }
                break;
            case 5:
                if (endI < needRepairNames.size()) {
                    showFristIndex += 6;
                    setEndI(Math.min(needRepairNames.size(), showFristIndex + 6));
                    resetUI();
                }
                break;
            case 6:
                fcv.setOpenSayShop(false);
                FCRMaster.fcv.setOpenSay(false);
                FCRMaster.fcv.setSayNpcName(null);
                FCRMaster.fcv.setSayNpcTextNum(0);
                break;
        }
    }


}
